//
//  PopoverPresentationController.swift
//  swift-GWYWeiBo
//
//  Created by x on 2017/3/15.
//  Copyright © 2017年 HLB. All rights reserved.
//

import UIKit
//这个类是负责专场的  管着转场的控制器
class PopoverPresentationController: UIPresentationController {

    var presentFrame = CGRect.zero
    
    /// 初始化方法 用于创建专场动画的对象
    ///
    /// - Parameters:
    ///   - presentedViewController: 被展现的控制器  (被弹出的控制器)
    ///   - presentingViewController: 发起的控制器 ?可选值  打印是nil
    override init(presentedViewController: UIViewController, presenting presentingViewController: UIViewController?) {
        super.init(presentedViewController: presentedViewController, presenting: presentingViewController)
//        print(presentedViewController)
//        print(presentingViewController ?? 9999)
    }
    
    
    /// 即将布局展现子视图的调用
    override func containerViewWillLayoutSubviews() {
        super.containerViewWillLayoutSubviews()
        //1.修改弹出视图的大小属性
        //containerView  容器的view视图
        //presentedView  展现view的视图
        //动态的修改frame;
        if presentFrame.equalTo(CGRect.zero) {
            presentedView?.frame = CGRect(x: 100, y: 56, width: 200, height: 200)
        }else{
            presentedView?.frame = presentFrame
        }
        //2.插入一个蒙版视图(把这个view插入到容器视图的最底层)
        containerView?.insertSubview(coverView, at: 0)
    }
    
    //MARK:- 懒加载view界面  
    //为什么懒加载View 不声明变量的类型  加载出来的view不能交互
    private lazy var coverView: UIView = {
        () ->UIView in
        let view = UIView(frame: UIScreen.main.bounds)
        view.backgroundColor = UIColor(white: 0.0, alpha: 0.2)
        
        let tap = UITapGestureRecognizer(target: self, action: #selector(tapGR))
        view.addGestureRecognizer(tap)
        
        return view
    }()
    //真正的私有方法
    func tapGR() -> Void {
        print(#function)
        //拿到当前弹出的那个控制器
        presentedViewController.dismiss(animated: true, completion: nil)
    }
}
